import java.util.Random;
import java.util.Scanner;

public class test1 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入n");
        int n = scan.nextInt();
        int[][] arr = new int[n][n];
        Random r = new Random();
        //记录每行最大值的下标和最大值
        int row, max ;
        for (int i = 0; i < n; i++) {
            //重置下标和最大值
            max = Integer.MIN_VALUE;row = -1;
            for (int j = 0; j < n; j++) {
                arr[i][j] = r.nextInt(500);
                //先输出第一小题的答案
                System.out.print(arr[i][j]+"\t");
                //比较并记录最大值
                if(arr[i][j]>max){
                    max = arr[i][j];
                    row = j;
                }
            }
            System.out.println();
            //再修改每行最大值的位置
            swap(arr[i],i,row);
        }
        System.out.println("*********************************");
        //打印第二小题的答案
        print(arr);
        //第三小题(冒泡排序)
        for(int i = 0;i<n;i++){
            //小优化：记录数组是否已有序
            boolean flag = false;
            for(int j = 0;j<n-1-i;j++){
                if(arr[j][j]>arr[j+1][j+1]){
                    int tmp = arr[j][j];
                    arr[j][j] = arr[j+1][j+1];
                    arr[j+1][j+1] = tmp;
                    flag = true;
                }
            }
            //小优化：如已有序就停止循环
            if(!flag){
                break;
            }
        }
        System.out.println("********************************");
        //打印第三题的答案
        print(arr);
    }
    public static void swap(int[] arr,int a,int b){
        int tmp = arr[a];
        arr[a] = arr[b];
        arr[b] = tmp;
    }
    public static void print(int[][] arr){
        for (int[] tmp:arr) {
            for (int num:tmp) {
                System.out.print(num+"\t");
            }
            System.out.println();
        }
    }
}
